package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.dto.output.PageOutputDto;
import com.b511.swpu.cementbackend.entity.Sys_Module_Data;
import com.b511.swpu.cementbackend.model.DataTable;
import com.b511.swpu.cementbackend.model.ModuleDataField;
import com.b511.swpu.cementbackend.repository.DataBrowserRepository;
import com.b511.swpu.cementbackend.repository.MetaFieldRepository;
import com.b511.swpu.cementbackend.repository.MetaTableRepository;
import com.b511.swpu.cementbackend.repository.Sys_Module_DataRepository;
import com.b511.swpu.cementbackend.service.DataBrowserService;
import com.b511.swpu.cementbackend.util.Help;
import com.b511.swpu.cementbackend.util.R;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataBrowserServiceImpl  implements DataBrowserService {

    private final Sys_Module_DataRepository module_dataRepository;

    public DataBrowserServiceImpl(Sys_Module_DataRepository module_dataRepository) {
        this.module_dataRepository = module_dataRepository;
    }

    @Override
    public R getDataByCondition(BoreholeConditionInputDto boreholeConditionInputDto, String moduleName, PageRequest pageRequest) throws Exception {
        DataBrowserRepository repository=new DataBrowserRepository();
        List<ModuleDataField> fields=new ArrayList<ModuleDataField>();
        // TODO: 获取moduleName，查表获取field，用于构造动态sql
        // 通过参数的方式传递？还是通过dto传过来
        // moduleName = "stratum";
        // System.out.println("moduleName" + moduleName);
        // 根据mpduleName查相关field
        if(moduleName!="" && moduleName!=null){
            List<Sys_Module_Data> module_data = module_dataRepository.findByModuleName(moduleName);
            // System.out.println(module_data);
            if(module_data.size() == 0){
                return R.error("该模块尚未配置!");
            }
            // 遍历赋值
            for(Sys_Module_Data module : module_data){
                ModuleDataField field = new ModuleDataField();
                field.setFieldName(module.getFieldName()).setTableName(module.getTableName());
                fields.add(field);
            }

    //        静态测试数据
    //        ModuleDataField field1=new ModuleDataField();
    //        field1.setFieldName("formation");field1.setTableName("STRATUM");
    //        fields.add(field1);
    //        ModuleDataField field2=new ModuleDataField();
    //        field2.setFieldName("member");field2.setTableName("STRATUM");
    //        fields.add(field2);

            // DataTable dt= repository.findAll(boreholeConditionInputDto,fields,"STRATUM");
            // String jsonStr =  repository.findAll(boreholeConditionInputDto,fields,"STRATUM");
            return R.ok().putData(repository.findAll(boreholeConditionInputDto,fields,moduleName, pageRequest));
            // return R.ok().putData(new PageOutputDto<>(dt.Rows.size(), dt.Rows));
        }
        return R.error("moduleName 输入错误!");
    }

    @Override
    public R getWellByCondition(BoreholeConditionInputDto boreholeConditionInputDto, PageRequest pageRequest) throws Exception {
        DataBrowserRepository repository=new DataBrowserRepository();
        return R.ok().putData(repository.findWell(boreholeConditionInputDto, pageRequest));
    }
}
